package com.zy.openchat.core.manager;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

public class RedPacketManager {
    private volatile static RedPacketManager redpackManaer;

    private RedPacketManager() {
    }

    public static RedPacketManager getRedPacketManager() {
        if (redpackManaer == null) {
            synchronized (RedPacketManager.class) {
                if (redpackManaer == null) {
                    redpackManaer = new RedPacketManager();
                }
            }
        }
        return redpackManaer;
    }
    private List<Long> openedRedIds = new ArrayList<>();
    private String spuRedIds = null;
    public void refresh(){
        openedRedIds.clear();
        spuRedIds = SPUtils.getInstance().getString("redOpenIds","");
        if(!TextUtils.isEmpty(spuRedIds)){
            String[] array = spuRedIds.split(":");
            if(array!=null&&array.length>0){
                for(String id:array){
                    try {
                        openedRedIds.add(Long.parseLong(id));
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public boolean isOpendById(long openedRedId){
        if(openedRedIds.isEmpty()){
            return false;
        }
        return openedRedIds.contains(openedRedId);
    }

    public void appendId(long openedRedId){
        if(openedRedId<=0){
            return;
        }
        if(!TextUtils.isEmpty(spuRedIds)){
            spuRedIds = spuRedIds+":"+openedRedId;
        }else{
            spuRedIds = ""+openedRedId;
        }
        SPUtils.getInstance().put("redOpenIds",spuRedIds);
        openedRedIds.add(openedRedId);
    }



    public void clean(){
        openedRedIds.clear();
        spuRedIds = null;
        SPUtils.getInstance().put("redOpenIds","");
    }
}
